package com.example.server.util;

import com.example.server.trasaction.HaeTransactionManager;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;

/**
 * @Auther: yuanjianpeng
 * @Date: 2019/5/18 23:28
 * @Description: 远程发送调用
 */
public class HttpClient {

    public static String get(String url) {

        String result = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet();
        httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
        httpGet.setHeader("groupId", HaeTransactionManager.getCurrentGroupId());
        httpGet.setHeader("transactionCount", String.valueOf(HaeTransactionManager.getTransactionCount()));

        try {
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);


            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
            }

            httpResponse.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
